package com.wokeMy.view.channe.changjie;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class CjSlyzmActivity extends BaseActivity {
    EditText yzm_cjiesl_et;
    String jiemian,bindid,userno,user_id;
    String yzm;
    Intent intent;
    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_cj_slyzm);
    }

    @Override
    public void initActivity() {
        Toast.makeText(CjSlyzmActivity.this,"短信验证码已经下发",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void initView() {
        myFindViewById(R.id.back_cjiesl_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        yzm_cjiesl_et=myFindViewById(R.id.yzm_cjiesl_et);
        myFindViewById(R.id.zhifu_cjiesl_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yzm=yzm_cjiesl_et.getText().toString().trim();
                switch (jiemian) {
                    case "cjieyzm":
                        //畅捷验证码
                        if (yzm.equals("")||yzm==null){
                            Toast.makeText(CjSlyzmActivity.this,"请填写短信验证码",Toast.LENGTH_SHORT).show();
                        }else{
                            Intent intent =new Intent();
                            intent.putExtra("smsCode_bind",yzm);
                            setResult (Constant.RESPONSE_CJKJZFBIND, intent);
                            finish ();
                        }
                        break;
                    case "creditRepay":
                        //信用卡还款 验证码
                        bindid=intent.getStringExtra("bindid");
                        userno=intent.getStringExtra("userno");
                        user_id=intent.getStringExtra("user_id");
                        if (yzm.equals("")||yzm==null){
                            Toast.makeText(CjSlyzmActivity.this,"请填写短信验证码",Toast.LENGTH_SHORT).show();
                        }else{
                            dialog=Util.createLoadingDialog(CjSlyzmActivity.this);
                            zctYzm();
                        }
                        break;
                    default:
                        break;
                }

            }
        });
    }

    @Override
    public void initData() {
         intent=getIntent();
        jiemian=intent.getStringExtra("jiemian");
    }
    //掌财信用卡验证码后开通  开通
    private void zctYzm() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("bind_id",bindid);
        params.put("sms_code",yzm);
        params.put("user_no",userno);
        Log.e("掌财信用卡验证码后开通params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.QYCREZCT_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        Log.e("掌财信用卡验证码后开通Result", result);
                        //                        Toast.makeText(CreditRepayActivity.this, result, Toast.LENGTH_SHORT).show();
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                                if (jsonObject.getString("flag").equals("success")) {
                                    if (jsonObject.getJSONObject("data").getJSONObject("data").getString("bindstatus").equals("01")){
                                        Toast.makeText(CjSlyzmActivity.this,"开通成功", Toast.LENGTH_LONG).show();
                                        finish();
                                    }else {
                                        tishiDialog("提示","开通失败，亲重新选卡开通！");
                                    }
                                } else {
                                    Toast.makeText(CjSlyzmActivity.this, jsonObject.getString("info"), Toast.LENGTH_LONG).show();
                                }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    AlertDialog alertDialog;
    private void tishiDialog(String info, String kll) {
        alertDialog = new AlertDialog.Builder(CjSlyzmActivity.this).
                setTitle(info).
                setMessage(kll).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create();
        alertDialog.show();
    }
}
